package com.ruoyi.framework.interceptor;

import com.ruoyi.framework.mdc.MdcConstain;
import com.ruoyi.framework.security.filter.ApiSignatureFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * api签名认证的拦截器
 *
 * @Author： Lonni
 * @Date： 2025/9/9  09:32
 */

public class ApiSignatureInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(ApiSignatureInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("api签名拦截器=======>");
        String method = request.getMethod();
        String requestURI = request.getRequestURI();
        String contentType = request.getContentType();
        if (contentType != null && contentType.startsWith("multipart/")) {
            return true;
        }
//TODO  注释调

//        ApiSignatureFilter apiSignatureFilter = new ApiSignatureFilter();
//        apiSignatureFilter.validateSignature(request);
        logger.info("校验签名成功");
        MDC.put(MdcConstain.IS_API,"true");
        MDC.put(MdcConstain.API_USER_ID,request.getHeader("userId"));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    }
}
